/**
 * 
 */
package com.yidu.springbootdemo.permission.action;

import com.yidu.springbootdemo.permission.action.vo.TcharacterVo;
import com.yidu.springbootdemo.permission.action.vo.TrolesModulesVo;
import com.yidu.springbootdemo.permission.domain.Tcharacter;
import com.yidu.springbootdemo.permission.domain.Tuser;
import com.yidu.springbootdemo.permission.service.TcharacterService;
import com.yidu.springbootdemo.permission.service.TrolesModulesService;
import com.yidu.springbootdemo.permission.tools.Messages;
import com.yidu.springbootdemo.permission.tools.Pages;
import com.yidu.springbootdemo.permission.tools.Tools;
import com.yidu.utils.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 *	类的描述：
 * @author  张韦超
 * @date 2019年9月27日 上午10:21:22
 */
@Controller
@RequestMapping("/character")
public class CharacterController {
	
	@Resource
	TcharacterService tcharacterService;

	@Resource
	RedisUtil redisUtil ;

	@Resource
	TrolesModulesService trolesModulesService;
	/**
	 * 	角色的增加和修改
	 * @return
	 */
	@RequestMapping("/addOrUp")
	@ResponseBody
	public Messages addOrUpdate(TcharacterVo tch,HttpServletRequest request) {
		Messages msg = new Messages();
		String id = request.getParameter("chaId");
		//tch.setChaFoundUser(request.getSession().getAttribute("user").toString());
		//得到登录的用户
		Tuser user = (Tuser) request.getSession().getAttribute("user");
		if (user == null) {
			tch.setChaFoundUser("hjj");
		} else {
			tch.setChaFoundUser(user.getUserName());
		}
		tch.setChaFoundDate(Tools.time());
		String rd = UUID.randomUUID().toString().replaceAll("-", "");
		tch.setChaId(rd);
		if(!StringUtils.isEmpty(tch.getChaStandby1())){
			tch.setChaStandby2("N");
		}else{
			tch.setChaStandby2("Y");
		}
		if(!StringUtils.isEmpty(id)) {
			//tch.setChaID(id);
			tch.setChaId2(id);
			trolesModulesService.delete(id);
			tcharacterService.update(id);
		}
		Tcharacter tc = new Tcharacter();
		BeanUtils.copyProperties(tch, tc);
		tcharacterService.add(tc);
		redisUtil.deleteByPrex("ours"); //删除所有的缓存
		msg.setStatus(1);
		msg.setMsg("成功");
		return msg;
	}
	/**
	 * 	主界面显示所有
	 * @param
	 * @return
	 */
	@RequestMapping("/selectAll")
	@ResponseBody
	public Map<String,Object> select(TcharacterVo tcharacter,HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();//得到一个新的 map集合
		//从页面上得到页数 和 limit
		int page = Integer.valueOf(request.getParameter("page"));
		int limit = Integer.valueOf(request.getParameter("limit"));
		//将 得到的页数 以及limit 放入到pages 的工具类中
		Pages pages = new Pages();
		pages.setCurPage(page);
		pages.setLimit(limit);
		//根据 页面上传来的 角色信息 去查找 总行数
		int totalRows = tcharacterService.count(tcharacter);
		//将总行数 放入到 pages 工具类中
		pages.setTotalRows(totalRows);
		//根据 角色信息 去查 角色
		List<TcharacterVo> list = tcharacterService.findAll(tcharacter,pages);//查数据
		// 遍历 list 集合 如果 chaState 为 Y 或者 N
		for (TcharacterVo tcharacter2 : list) {
			if(tcharacter2.getChaState().equals("Y")) {
				tcharacter2.setChaState("是");
			}else {
				tcharacter2.setChaState("否");
			}
		}
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", totalRows);
		map.put("data", list);
		return map;
	}
	/**
	 * 	真实删除
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Messages delete(String chaId) {
		Messages msg = new Messages();
		trolesModulesService.delete(chaId);
		tcharacterService.update(chaId);
		redisUtil.deleteByPrex("ours"); //删除所有的缓存
		msg.setStatus(1);
		msg.setMsg("成功");
		return msg;
	}
	
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder){
			binder.registerCustomEditor(Date.class, new CustomDateEditor(
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),true));
	}
	/**
	 *  	修改出现内容
	 */
	@RequestMapping("/selectOne")
	@ResponseBody
	public Map<String,Object> selectOne(String chaId) {
		Map<String,Object> map = new HashMap<String, Object>();//得到新的map集合
		TcharacterVo tc = tcharacterService.findByID(chaId);//根据 角色id查找角色信息
		List<TrolesModulesVo> list = trolesModulesService.findAll(chaId);//根据角色id查找角色模块表findAll
		String one = "";//定义个空的String
		//循环list集合
		for(int i = 0;i<list.size();i++) {
			TrolesModulesVo tr = list.get(i);
			//将集合中的 modId 都加入到 String 中去
			if(i == list.size()-1) {
				one+=tr.getModId();
			}else {
				one+=tr.getModId()+",";
			}
		}
		map.put("tc", tc);
		map.put("one", one);
		return map;
	}
}